new Vue({
  el: '#app',
  data() {
    return {
      form: {
        page: 1,			//页数
        limit: 10,		//每页数据条数
        Article_Name: "",
        Activity: ""
      },
      formData: {},
      tableData: [],
      tableData2: [],
      //表格数据
      total: 1,
      dialogForm: false,
      title: '',
      multipleSelection: []
    }
  },
  mounted() {
    this.getList(this.form)
    console.log(DomainName);
  },
  methods: {
    onSerach() {				//查询方法
      this.getList(this.form)
    },
    addContract() {
      console.log("跳转")
    },
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.limit = val;
      this.getList(this.form)
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form)
    },
    getList(data) {
      let _this = this
      $.ajax({
        type: 'POST',
        data,
        url: DomainName + '/getSalesUnitPriceAction?token=' + $.cookie("token"),
        success: function (data) {
          _this.tableData = data.tableData;
          _this.total = Number(data.limit);
          console.log(_this.tableData)
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    expandChange(row) {
      let data = row.Sales_Unit_Price_SerialNumber
      let _this = this
      $.ajax({
        type: 'POST',
        data: {
          Sales_Unit_Price_SerialNumber: data
        },
        url: DomainName + '/getSalesUnitPricDetail?token=' + $.cookie("token"),
        success: function (data) {
          row.Children.sales_unitprice_detail = data.Data;
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    addEnterprise() {
      window.location.href = './Add.html'
    },
    showMember(Sales_Unit_Price_SerialNumber) {
      sessionStorage.setItem("Sales_Unit_Price_SerialNumber", Sales_Unit_Price_SerialNumber)
      window.location.href = './show.html?Sales_Unit_Price_SerialNumber=' + Sales_Unit_Price_SerialNumber
    },
    editMember(Sales_Unit_Price_SerialNumber) {
      sessionStorage.setItem("Sales_Unit_Price_SerialNumber", Sales_Unit_Price_SerialNumber)
      window.location.href = './Edit.html?Sales_Unit_Price_SerialNumber=' + Sales_Unit_Price_SerialNumber
    },
    onExcel() {
      /* generate workbook object from table */
      //  .table要导出的是哪一个表格
      var wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));
      /* get binary string as output */
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        //  name+'.xlsx'表示导出的excel表格名字
        saveAs(
          new Blob([wbout], {
            type: "application/octet-stream"
          }),
          "销售价格单" + ".xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },
    deletMember(Sales_Unit_Price_SerialNumber) {
      this.$confirm('此操作将删除该销售价格单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.delete(Sales_Unit_Price_SerialNumber);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    delete(Sales_Unit_Price_SerialNumber) {
      const _this = this
      $.ajax({
        url: DomainName + '/SalesUnitpriceId',
        type: 'POST',
        data: { Data: Sales_Unit_Price_SerialNumber },
        success: function (res) {
          if (res.length == 1) {
            res[0].Row_Status = '1'
            let data = { Data: JSON.stringify({ Modify: { sales_unitprice: [res[0]] } }) };
            $.ajax({
              url: DomainName + '/GoodsFileEdit',
              type: 'POST',
              data,
              success: function (re) {
                if (re.Status == "success") {
                  _this.$message.success("作废成功")
                  _this.getList();
                }
              },
              error: function () {
                _this.$message.error("删除失败，请稍后再试")
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              }
            })
          }
        }
      })
    },
    // 子表删除
    deletMember2(row) {
      const _this = this;
      let Primary_Key_Name = "Sales_Unit_Price_Detail_SerialNumber";
      let Primary_Key = row.Sales_Unit_Price_Detail_SerialNumber;
      this.$confirm('此操作将删除该商品信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        $.ajax({
          url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie('token'),
          type: 'POST',
          data: {
            Primary_Key_Name,
            Primary_Key,
            Data: "sales_unitprice_detail",
          },
          error: function (array) {
            console.log(array);
            _this.$message.error("删除失败，请稍后再试")
          },
          complete: function (XMLHttpRequest) {
            _this.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          },
          success: function (res) {
            _this.Delete(res);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '已取消删除'
        });
      });
    },
    //删除
    Delete(row) {
      const _this = this;
      let rowData = [];
      // 判断 row 的类型
      if (Array.isArray(row)) {
        // 如果是数组，则取第一个元素作为 rowData
        rowData = [...row];
      } else {
        // 否则，直接使用 row 作为 rowData
        rowData.push(
          row
        );
      }
      rowData.forEach((item) => {
        item.Row_Status = '1'
      })
      let data = {
        Data: JSON.stringify({
          Modify: { sales_unitprice_detail: rowData }
        })
      };
      $.ajax({
        url: DomainName + '/getEditAction?token=' + $.cookie('token'),
        type: 'POST',
        data,
        error: function (array) {
          console.log(array);
          _this.$message.error("删除失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (res) {
          if (res.Status == 'success') {
            _this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        }
      })
    }
  },
})